
#include "material.h"

Cmaterial::Cmaterial(const string& name)
{
 ifstream plik;
 string word,w1,w2,w3;
 int lvl=0;
 int c=0;
 int m=0;
 int s=0;

 this->diffuseMap.erase();

 this->vp.erase();
 this->gp.erase();
 this->fp.erase();

 plik.open(name.c_str(), ifstream::in);
 lvl=0;
 while(plik.good())
      {
      plik >> word;
	  if (word == "{") lvl++;
	  if (word == "}") 
	     {
		 if (lvl == 2 && c == 1) c=0;
		 if (lvl == 2 && m == 1) m=0;
         if (lvl == 2 && s == 1) s=0;
		 lvl--;
         }

	  if (word == "color"   && lvl == 1) c=1;
	  if (word == "maps"    && lvl == 1) m=1;
	  if (word == "shaders" && lvl == 1) s=1;

	  if (word == "ambient" && c == 1)
	     {
         plik >> word;
		 plik >> w1;
		 plik >> w2;
		 plik >> w3;
		 this->a.r = (float)atof(w1.c_str());
		 this->a.g = (float)atof(w2.c_str());
		 this->a.b = (float)atof(w3.c_str());
		 plik >> word;
	     }

	  if (word == "diffuse" && c == 1)
	     {
         plik >> word;
		 plik >> w1;
		 plik >> w2;
		 plik >> w3;
		 this->diffuse.r = (float)atof(w1.c_str());
		 this->diffuse.g = (float)atof(w2.c_str());
		 this->diffuse.b = (float)atof(w3.c_str());
		 plik >> word;
	     }

      if (word == "specular" && c == 1)
	     {
         plik >> word;
		 plik >> w1;
		 plik >> w2;
		 plik >> w3;
		 this->s.r = (float)atof(w1.c_str());
		 this->s.g = (float)atof(w2.c_str());
		 this->s.b = (float)atof(w3.c_str());
		 plik >> word;
	     }

	  if (word == "diffuse" && m == 1)
	     {
         plik >> word;  // {
		 plik >> word;
		 this->diffuseMap = word;
		 //enTextures.load(word);
		 plik >> word;  // }
	     }

	  if (word == "vertex" && s == 1)
	     {
         plik >> word;
		 plik >> word;
		 this->vp = word;
//		 enShadersAdd(VERTEX,word);
		 plik >> word;
	     }

	  if (word == "geometry" && s == 1)
	     {
         plik >> word;
		 plik >> word;
		 this->gp = word;
//		 enShadersAdd(GEOMETRY,word);
		 plik >> word;
	     }

	  if (word == "fragment" && s == 1)
	     {
         plik >> word;
		 plik >> word;
		 this->fp = word;
//		 enShadersAdd(FRAGMENT,word);
		 plik >> word;
	     }
      }

 plik.close();
}